package org.ucas.kgraph.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ucas.kgraph.dao.RelationDao;
import org.ucas.kgraph.dao.SchemDao;
import org.ucas.kgraph.dto.GraphDto;
import org.ucas.kgraph.dto.ListDto;
import org.ucas.kgraph.entity.Relation;
import org.ucas.kgraph.entity.RelationType;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.entity.Term;
import org.ucas.kgraph.entity.TermGraph;
import org.ucas.kgraph.utils.exception.BadRequestException;
import org.ucas.kgraph.utils.exception.ConflictException;
import org.ucas.kgraph.utils.exception.ForbiddenException;
import org.ucas.kgraph.utils.exception.NotFoundException;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Transactional
@Service
public class SchemService {

  @Autowired
  private SchemDao schemDao;

  public Schem save(Schem t) {
    return schemDao.save(t);
  }
  
  public Schem create(Schem t){
	return save(t);
  }
  
  public List<Schem> listRootAll(String type,String state,Long file_id){
	return schemDao.findByAll(type,state,file_id);
  } 
  public List<Schem> listRootByUser(Long account_id,String type,String state,Long file_id){
	//if(account_id==null) account_id = Long.valueOf(0);
	return schemDao.findByAccountId(account_id,type,state,file_id);
  }
  public Schem load(Long id) {
	    return schemDao.findOne(id);
  }
  public Schem recontent(Long id, String content,String root,String theme) {
	// TODO Auto-generated method stub
	Schem schem = load(id);
	if(schem == null) throw NotFoundException.Term_NotExists;
	schem.setContent(content);
	schem.setRoot(root);
	schem.setTheme(theme);
	return schemDao.save(schem);
	
  }
  public Schem reroot(Long id, String newroot) throws JsonParseException, JsonMappingException, IOException {
		// TODO Auto-generated method stub
	Schem schem = load(id);
	if(schem == null) throw NotFoundException.Term_NotExists;
	String content = schem.getContent();
	ObjectMapper mapper = new ObjectMapper();  
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);  
	GraphDto graph = mapper.readValue(content, GraphDto.class); 
	for(int i=0;i<graph.getData().size();i++){
		 if(graph.getData().get(i).getParentid()==null){
			 graph.getData().get(i).setTopic(newroot);
		 }
	}
	content=mapper.writeValueAsString(graph);
	schem.setContent(content);
	schem.setRoot(newroot);
	return schemDao.save(schem);
  }
  public void delete(Long id) {
	// TODO Auto-generated method stub
	schemDao.delete(id);
  }

  public Schem submitm(Long id) {
	// TODO Auto-generated method stub
	return null;
  }
 
  public Schem submitp(Long id, Long file_id) {
	// TODO Auto-generated method stub
	Schem schem = load(id);
	if(schem == null) throw NotFoundException.Term_NotExists;
	
	schem.setState("1");
	schem.setFile_id(file_id);
	return schemDao.save(schem);
  }

  public List<Schem> searchByRoot(String root,String type, String state) {
	// TODO Auto-generated method stub
    return schemDao.searchByRoot(root,type,state);
	
  }

  public List<Schem> listRootAllByFile(Long id) {
	 return schemDao.findByAllByFile(id);
  } 

  public List listRootBySearch(Long account_id, String type, String state, Long file_id, String searchString) {
	// TODO Auto-generated method stub
	  return schemDao.findByAll(type,state,file_id,searchString);
  }

  
}
